package com.xfcycle.zebrabikegp.customView;

import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.xfcycle.zebrabikegp.R;
import com.xfcycle.zebrabikegp.util.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义从屏幕底部弹出的PopWindow
 * Created by xFcycle on 2017/5/24.
 */

public abstract class BottomPopWindow {
    private static final String TAG = "BottomPopWindow";

    private Activity mActivity;
    private View mViewAnchor;
    private LayoutInflater mInflater;
    private Window mWindow;
    private WindowManager.LayoutParams params;
    private PopupWindow mPopupwindow;

    private TextView tv_bottom_pop_window_zero;
    private TextView tv_bottom_pop_window_one;
    private TextView tv_bottom_pop_window_two;
    private TextView tv_bottom_pop_window_three;
    private TextView tv_bottom_pop_window_four;
    private TextView tv_bottom_pop_window_five;

    private LinearLayout ll_bottom_pop_window_zero;
    private LinearLayout ll_bottom_pop_window_one;
    private LinearLayout ll_bottom_pop_window_two;
    private LinearLayout ll_bottom_pop_window_three;
    private LinearLayout ll_bottom_pop_window_four;
    private LinearLayout ll_bottom_pop_window_five;


    private TextView tv_bottom_pop_window_cancel;

    private List<TextView> textViews;
    private List<LinearLayout> linearLayouts;


    public BottomPopWindow(Activity activity, View viewAnchor,String[] texts) {
        this.mActivity = activity;
        this.mViewAnchor = viewAnchor;
        this.mInflater = LayoutInflater.from(activity);
        this.params = activity.getWindow().getAttributes();
        this.mWindow = activity.getWindow();

        initView();
        initViewListener();
        setLineTextByLineId(texts);
    }

    private void initView() {

        textViews = new ArrayList<>();
        linearLayouts = new ArrayList<>();

        Log.e(TAG, "initView: " + mInflater);
        View view = mInflater.inflate(R.layout.fragment_bottom_pop_window, null);
        params.dimAmount = 0.5f;
        mWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        //利用PopupWindow展示自定义菜单栏
        mPopupwindow = new PopupWindow(view, ScreenUtil.getScreenSize(mActivity).get("screenWidth"), LinearLayout.LayoutParams.WRAP_CONTENT);

        //设置宽高
        mPopupwindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        mPopupwindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        //设置能够被点击
        mPopupwindow.setTouchable(true);
        mPopupwindow.setFocusable(true);
        mPopupwindow.setOutsideTouchable(true);
        //设置动画效果
        mPopupwindow.setAnimationStyle(R.style.popwin_anim_style);

        ll_bottom_pop_window_zero = (LinearLayout) view.findViewById(R.id.ll_bottom_pop_window_zero);
        ll_bottom_pop_window_one = (LinearLayout) view.findViewById(R.id.ll_bottom_pop_window_one);
        ll_bottom_pop_window_two = (LinearLayout) view.findViewById(R.id.ll_bottom_pop_window_two);
        ll_bottom_pop_window_three = (LinearLayout) view.findViewById(R.id.ll_bottom_pop_window_three);
        ll_bottom_pop_window_four = (LinearLayout) view.findViewById(R.id.ll_bottom_pop_window_four);
        ll_bottom_pop_window_five = (LinearLayout) view.findViewById(R.id.ll_bottom_pop_window_five);

        tv_bottom_pop_window_zero = (TextView) view.findViewById(R.id.tv_bottom_pop_window_zero);
        tv_bottom_pop_window_one = (TextView) view.findViewById(R.id.tv_bottom_pop_window_one);
        tv_bottom_pop_window_two = (TextView) view.findViewById(R.id.tv_bottom_pop_window_two);
        tv_bottom_pop_window_three = (TextView) view.findViewById(R.id.tv_bottom_pop_window_three);
        tv_bottom_pop_window_four = (TextView) view.findViewById(R.id.tv_bottom_pop_window_four);
        tv_bottom_pop_window_five = (TextView) view.findViewById(R.id.tv_bottom_pop_window_five);

        tv_bottom_pop_window_cancel = (TextView) view.findViewById(R.id.tv_bottom_pop_window_cancel);

        textViews.add(tv_bottom_pop_window_zero);
        textViews.add(tv_bottom_pop_window_one);
        textViews.add(tv_bottom_pop_window_two);
        textViews.add(tv_bottom_pop_window_three);
        textViews.add(tv_bottom_pop_window_four);
        textViews.add(tv_bottom_pop_window_five);

        linearLayouts.add(ll_bottom_pop_window_zero);
        linearLayouts.add(ll_bottom_pop_window_one);
        linearLayouts.add(ll_bottom_pop_window_two);
        linearLayouts.add(ll_bottom_pop_window_three);
        linearLayouts.add(ll_bottom_pop_window_four);
        linearLayouts.add(ll_bottom_pop_window_five);

    }

    private void initViewListener() {
        //监听PopupWindow，当其消失时屏幕恢复亮度
        mPopupwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                params.alpha = 1.0f;
                mWindow.setAttributes(params);
            }
        });

        tv_bottom_pop_window_zero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lineZeroListener();
            }
        });

        tv_bottom_pop_window_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lineOneListener();
            }
        });

        tv_bottom_pop_window_two.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lineTwoListener();
            }
        });

        tv_bottom_pop_window_three.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lineThreeListener();
            }
        });

        tv_bottom_pop_window_four.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lineFourListener();
            }
        });

        tv_bottom_pop_window_five.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lineFiveListener();
            }
        });

        tv_bottom_pop_window_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissing();
            }
        });
    }

    //展示底部对话框
    public void showing() {
        mPopupwindow.showAtLocation(mViewAnchor, Gravity.BOTTOM, 0, 0);
        params.alpha = 0.5f;
        mWindow.setAttributes(params);
    }

    //关闭底部对话框
    public void dismissing() {
        if (mPopupwindow != null && mPopupwindow.isShowing()) {
            mPopupwindow.dismiss();
        }
    }

    /**
     * 改变文字内容
     *
     * @param texts 需要改变的文字内容 数组 数组长度最大不超过6行
     */
    private void setLineTextByLineId(String[] texts) {

        if (texts.length > textViews.size()) {
            Log.e(TAG, "setLineTextByLineId: 修改的展示列的数量大于本身所拥有的的6行 无法修改");
            return;
        }

        dismissLine(texts.length);

        for (int i = 0; i < texts.length; i++) {
            textViews.get(i).setText(texts[i]);
        }
    }

    /**
     * 隐藏某行
     *
     * @param lineNum 需要展示的行的数目
     */
    private void dismissLine(int lineNum) {
        for (int i = linearLayouts.size() - 1; i > lineNum - 1; i--) {
            linearLayouts.get(i).setVisibility(View.GONE);
        }

    }

    //添加监听
    public abstract void lineZeroListener();

    public abstract void lineOneListener();

    public abstract void lineTwoListener();

    public abstract void lineThreeListener();

    public abstract void lineFourListener();

    public abstract void lineFiveListener();

}